package com.roger.ats.init.util;

import com.roger.uia.helper.UiBase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class CfgUtils {

    public String getCfg(String startTag){
        String s ="";
        try {
            File file = new File(UiBase.filePath+"ATS/config/settings.ini");
            FileInputStream is = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            String line = "";
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = br.readLine()) != null) {
                if(line.startsWith(startTag)){
                    stringBuilder.append(line);
                }
            }
            s = stringBuilder.toString().replaceAll(startTag,"");
            br.close();
            is.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        return s;
    }

   public String[] getPacInfo(String path){
        String[] pac = null;
        try {
            File file = new File(path);
            FileInputStream is = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
            String line = "";
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = br.readLine()) != null) {
                if(line.contains("=") && !line.startsWith("#")){
                    stringBuilder.append(line.substring(line.indexOf("=")).replaceAll("=",""));
                    stringBuilder.append("\n");
                }
            }
            pac=stringBuilder.toString().split("\n");
            br.close();
            is.close();
        }catch (IOException e){
            e.printStackTrace();
        }
        return pac;
    }


    public String[] getCfg(String startTag,String split){
        return getCfg(startTag).split(split);
    }
}
